#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <osal/osal.h>
#include <api/libosd/osd_lib.h>
#include <api/libtsi/db_3l.h>
#include <api/libdiseqc/lib_diseqc.h>
#include <math.h>

#include "osdobjs_def.h"
#include "osd_config.h"
#include "string.id"
#include "images.id"
#include "vkey.h"
#include "key.h"
#include "control.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_popup_smsg.h"
#if	defined( SUPPORT_CAS9) 
#include "conax_ap/win_ca_mmi.h"
#endif

#if defined(SUPPORT_CAS7)
#include "conax_ap7/win_ca_mmi.h"
#endif
#define POP_PRINTF PRINTF
#define POPUP_MAX_TITLE_LEN	30

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
#include "abel_ca/win_abel_mmi.h"
#define POPUP_MAX_MSG_LEN	512
#else
#define POPUP_MAX_MSG_LEN	300
#endif

#ifndef SD_UI
#define MAX_LEN_CONAX_ONE_LINE    	600
#else
#define MAX_LEN_CONAX_ONE_LINE    	300
#endif
#define POPUP_MAX_TIMER_STR_LEN 10
/*******************************************************************************
*	WINDOW's objects declaration
*******************************************************************************/
extern CONTAINER    g_win_popup;
extern TEXT_FIELD	win_popup_title_txt;
extern  MULTI_TEXT	win_popup_msg_mbx;
extern MULTI_TEXT	win_popup_msg_mbx_timer;//modify for adding welcome page when only open dvbt 2011 10 17
extern TEXT_FIELD	win_popup_yes;
extern TEXT_FIELD	win_popup_no;
extern TEXT_FIELD	win_popup_cancel;


UINT16 msg_title[POPUP_MAX_MSG_LEN];
UINT16 msg_str[POPUP_MAX_MSG_LEN];
TEXT_CONTENT popup_mtxt_content = 
    {STRING_ID, 0};
//modify for adding welcome page when only open dvbt 2011 10 17
TEXT_CONTENT popup_mtxt_content_timer = {STRING_ID, 0};
UINT16 msg_timer_str[POPUP_MAX_TIMER_STR_LEN];
static ID popup_timer_id = OSAL_INVALID_ID;
UINT16 cnt_poptime;
    
static VACTION popup_btn_text_keymap(POBJECT_HEAD pObj, UINT32 vkey);
static PRESULT popup_btn_text_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION popup_con_keymap(POBJECT_HEAD pObj, UINT32 vkey);
static PRESULT popup_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
/*******************************************************************************
*	WINDOW's objects defintion MACRO
*******************************************************************************/
/////////style
#define POPUPWIN_IDX	    WSTL_POP_WIN_01_HD
#define POPUPSH_IDX	        WSTL_BUTTON_POP_SH_HD
#define POPUPHL_IDX	        WSTL_BUTTON_POP_HL_HD
#define POPUP_TITLE_SH_IDX	WSTL_POP_TXT_SH_HD 
#define POPUPSTR_IDX  	    WSTL_POP_TXT_SH_01_HD 

#define POPUPWIN_IDX_SD	    WSTL_POP_WIN_01_HD//sharon WSTL_POP_WIN_01_8BIT
#define POPUPSH_IDX_SD	     WSTL_BUTTON_POP_SH_8BIT
#define POPUPHL_IDX_SD	      WSTL_BUTTON_POP_HL_8BIT
#define POPUP_TITLE_SH_IDX_SD	WSTL_POP_TEXT_8BIT
#define POPUPSTR_IDX_SD  	    WSTL_POP_TEXT_8BIT





#define LDEF_TXT_TITLE(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, POPUP_TITLE_SH_IDX, POPUP_TITLE_SH_IDX, 0,0,   \
    NULL, NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_MULTITEXT(root,varMsg,nxtObj,l,t,w,h)  \
    DEF_MULTITEXT(varMsg,root,nxtObj,C_ATTR_ACTIVE,0,   \
        0,0,0,0,0, l,t,w,h,POPUPSTR_IDX,POPUPSTR_IDX,POPUPSTR_IDX,POPUPSTR_IDX,  \
        NULL,NULL,  \
        C_ALIGN_CENTER | C_ALIGN_VCENTER,1, 0,0,w,h, NULL,&popup_mtxt_content)

//modify for adding welcome page when only open dvbt 2011 10 17
#define LDEF_MULTITEXT_TIMER(root,varMsg,nxtObj,l,t,w,h)  \
    DEF_MULTITEXT(varMsg,root,nxtObj,C_ATTR_ACTIVE,0,   \
	0,0,0,0,0, l,t,w,h,POPUPSTR_IDX,POPUPSTR_IDX,POPUPSTR_IDX,POPUPSTR_IDX,  \
	NULL,NULL,  \
	C_ALIGN_CENTER | C_ALIGN_VCENTER,1, 0,0,w,h, NULL,&popup_mtxt_content_timer)
//modify end

#define LDEF_TXT_BTM(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, POPUPSH_IDX, POPUPHL_IDX, 0,0,   \
    popup_btn_text_keymap, popup_btn_text_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_CONTAINER(varCon,nxtObj,l,t,w,h,focusID)   \
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, POPUPWIN_IDX,POPUPWIN_IDX,0,0,   \
    popup_con_keymap,popup_con_callback,  \
    nxtObj, focusID,0)

#ifndef SD_UI
#define	W_W     	540
#define	W_H     	360
#define W_L 		GET_MID_L(W_W)
#define W_T 		GET_MID_T(W_H)

#define W_MSG_L 	GET_MID_L(W_MSG_W)
//#ifdef SUPPORT_CAS9

#if defined(SUPPORT_CAS9) || defined(SUPPORT_CAS7)
#define W_MSG_T 	250
#else
#define W_MSG_T 	GET_MID_T(W_MSG_H)
#endif

#define W_MSG_W 	500
#define W_MSG_H 	200

#define W_CHS_L 	400
#define W_CHS_T 	300
#define W_CHS_W 	500
#define W_CHS_H 	200

#define MSG_L 		(W_L + 20)
#define MSG_T 		(W_T + 40)
#define MSG_W 		(W_W - 40)
#define MSG_H		80

#define BTM_L 		(W_L +  W_W - (BTM_W + BTM_GAP) * 3)
#define BTM_T  		(W_T + W_H - BTM_H*2)
#define BTM_W 		140
#define BTM_H 		38 //40
#define BTM_GAP 	10
#else
#define	W_W     	300
#define	W_H     	200
#define W_L 		200//GET_MID_L(W_W)
#define W_T 		150//GET_MID_T(W_H)

#define W_MSG_L 	184//GET_MID_L(W_MSG_W)
#define W_MSG_T 	152//250

#define W_MSG_W 	300
#define W_MSG_H 	120

#define W_CHS_L 	150
#define W_CHS_T 	155
#define W_CHS_W 	350
#define W_CHS_H 	150

#define MSG_L 		(W_L + 5)
#define MSG_T 		(W_T + 30)
#define MSG_W 		(W_W - 10)
#define MSG_H		60

#define BTM_L 		(W_L +  W_W - (BTM_W + BTM_GAP) * 3)
#define BTM_T  		(W_T + W_H - BTM_H*2)
#define BTM_W 		70
#define BTM_H 		30 //40
#define BTM_GAP 	20
#endif

#define POPUP_YES_ID    1
#define POPUP_NO_ID     2
#define POPUP_CANCEL_ID 3
/*******************************************************************************
*	WINDOW's objects defintion
*******************************************************************************/
LDEF_TXT_TITLE(&g_win_popup,win_popup_title_txt,&win_popup_msg_mbx, W_L + 10, W_T, W_W + 10, 40,0,msg_title)
//modify for adding welcome page when only open dvbt 2011 10 17
//LDEF_MULTITEXT(&g_win_popup, win_popup_msg_mbx, &win_popup_yes, MSG_L,MSG_T,MSG_W,MSG_H)
LDEF_MULTITEXT(&g_win_popup, win_popup_msg_mbx, &win_popup_msg_mbx_timer, MSG_L,MSG_T,MSG_W,MSG_H)
LDEF_MULTITEXT_TIMER(&g_win_popup, win_popup_msg_mbx_timer, &win_popup_yes, MSG_L,MSG_T+MSG_H,MSG_W,MSG_H)
//modify for adding welcome page when only open dvbt 2011 10 17
LDEF_TXT_BTM(&g_win_popup, win_popup_yes, &win_popup_no, 	1, 3, 2, 1, 1, BTM_L,BTM_T, BTM_W, BTM_H,  RS_COMMON_YES, NULL)
LDEF_TXT_BTM(&g_win_popup, win_popup_no, &win_popup_cancel, 2, 1, 3, 2, 2, BTM_L + (BTM_W + BTM_GAP)*1,BTM_T, BTM_W, BTM_H,  RS_COMMON_NO, NULL)
LDEF_TXT_BTM(&g_win_popup, win_popup_cancel, NULL, 			3, 2, 1, 3, 3, BTM_L + (BTM_W + BTM_GAP)*2,BTM_T, BTM_W, BTM_H,  RS_COMMON_CANCEL, NULL)

LDEF_CONTAINER(g_win_popup, &win_popup_title_txt, W_L, W_T, W_W, W_H, 1)
/*******************************************************************************
*	Local Variable & Function declare
*******************************************************************************/
static win_popup_type_t win_popup_type = WIN_POPUP_TYPE_SMSG;
static win_popup_choice_t win_popup_choice = WIN_POP_CHOICE_NULL;
static UINT8 save_back_ok;
struct Rect timer_rect;//modify for adding welcome page when only open dvbt 2011 10 17

#if (defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX))
extern UINT32 gdwWmDur;
#endif

static INT8 set_change_focus = -1; 
#ifndef SD_UI
#define MAX_LEN_ONE_LINE    	500
#define MIN_WIDTH_2BTN      	500
#define MIN_WIDHT_1ORNOBTN   	400

#define MSG_VOFFSET_NOBTN      	60
#define MSG_VOFFSET_WITHBTN     50//40
#define MSG_LOFFSET             40   
#define MSG_BUTTON_TOPOFFSET    40
#define MSG_BUTTON_BOTOFFSET    40

#define DEFAULT_FONT_H      	36
#define TITLE_H					36
#else
#define MAX_LEN_ONE_LINE    	300
#define MIN_WIDTH_2BTN      	250
#define MIN_WIDHT_1ORNOBTN   	200

#define MSG_VOFFSET_NOBTN      	24
#define MSG_VOFFSET_WITHBTN     16//40
#define MSG_LOFFSET             10   
#define MSG_BUTTON_TOPOFFSET    16
#define MSG_BUTTON_BOTOFFSET    24

#define DEFAULT_FONT_H      	24
#define TITLE_H					26

#endif

UINT16 msg_btn_str[3][10];

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
#include "menus_root.h"
#include <api/libcas/abel/abel.h>
#include <api/libcas/abel/abel_mmi.h>

//#define ABEL_PRINTF	libc_printf
#define ABEL_PRINTF

extern TEXT_FIELD win_popup_item_name;
extern TEXT_FIELD win_popup_item_val;
UINT16 msg_popup_item_name[POPUP_MAX_MSG_LEN];
UINT16 msg_popup_item_val[POPUP_MAX_MSG_LEN];

#define ITEM_TXT_L	W_L + MSG_LOFFSET
#define ITEM_TXT_T	BTM_T-BTM_H
#define ITEM_TXT_W	(W_W - MSG_LOFFSET * 2)/2
#define ITEM_TXT_H 	TITLE_H

#define ITEM_VAL_L	ITEM_TXT_L+ITEM_TXT_W
#define ITEM_VAL_T 	(W_T + W_H - BTM_H*2)
#define ITEM_VAL_W	ITEM_TXT_W
#define ITEM_VAL_H	ITEM_TXT_H

#define LDEF_TXT_ITEM_VAL(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WSTL_TEXT_29_HD, WSTL_TEXT_29_HD, 0,0,   \  
    NULL, NULL,  \
     C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)
     
#define LDEF_TXT_ITEM_NAME(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, POPUPSTR_IDX_SD, POPUPSTR_IDX_SD, 0,0,   \  
    NULL, NULL,  \
     C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

LDEF_TXT_ITEM_NAME ( &g_win_popup, win_popup_item_name, &win_popup_yes, ITEM_TXT_L, ITEM_TXT_T, ITEM_TXT_W, ITEM_TXT_H, 0, NULL )
LDEF_TXT_ITEM_VAL ( &g_win_popup, win_popup_item_val, &win_popup_yes, ITEM_VAL_L, ITEM_VAL_T, ITEM_VAL_W, ITEM_VAL_H, 0, NULL )


#define VACT_TVPIN_DEL_ONE_CHAR (VACT_PASS+1)

UINT8			g_input_tvpin_pos=0;
UINT8			g_input_tvpin_chars[8];
UINT8			g_target_tvpin_for_confirm_ui[8];
UINT8			g_target_parental_control_level_for_confirm_ui;
UINT8			g_tvpin_display[16];
BOOL			g_tvpin_confirm_mode=FALSE;
BOOL			g_tvpin_confirm_proc_exec_result=FALSE;
UINT16 			val_Xoffset = 0;
BOOL                    g_enter_wrong_pin_mmi = FALSE;

void _update_popup_item_tvpin_display(void)
{

	switch(g_input_tvpin_pos)
	{
		case 0:
			sprintf(g_tvpin_display,"----");
			break;			
		case 1:
			sprintf(g_tvpin_display,"*---");
			break;			
		case 2:
			sprintf(g_tvpin_display,"**--");
			break;			
		case 3:
			sprintf(g_tvpin_display,"***-");
			break;
		case 4:
			sprintf(g_tvpin_display,"****");
			break;
	}
	OSD_SetTextFieldContent((POBJECT_HEAD)&win_popup_item_val, STRING_ANSI, g_tvpin_display);
	OSD_DrawObject((POBJECT_HEAD)&win_popup_item_val,C_UPDATE_ALL);
}

#endif


static void  win_popup_Init();
static UINT8 win_popup_proc(UINT32 msgtype, UINT32 msgcode);
static const UINT8 win_popup_btm_num[] =
{
	0,
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
 	0,			
#endif	
	1,
	2,
	3
};
//modify for adding welcome page when only open dvbt 2011 10 17
char* win_popup_timer_count[] =
{
	"1",
	"2",
	"3",
	"4",
	"5",
	"6",
	"7",
	"8",
	"9",
	"10",
	"11",
	"12",
	"13",
	"14",
	"15",
};
//modify end
static OBJECT_HEAD* win_popup_btm_objs[] = 
{
	(OBJECT_HEAD*)&win_popup_yes,
	(OBJECT_HEAD*)&win_popup_no,
	(OBJECT_HEAD*)&win_popup_cancel
};

static UINT16 btn_str_ids[] =
{
	RS_COMMON_YES,
	RS_COMMON_NO,
	RS_COMMON_CANCEL
};
static UINT16 mp_btn_ids[] = 
{
	RS_MP_MUSIC,//RS_MP_MUSIC_PLAYLIST,
	RS_MP_IMAGE,//RS_MP_IMAGE_PLAYLIST,
	RS_COMMON_CANCEL
};
void cas_pop_stop_timer();
/*******************************************************************************
*	Window's keymap, proc and  callback
*******************************************************************************/
static VACTION popup_con_keymap(POBJECT_HEAD pObj, UINT32 vkey)
{
	VACTION act = VACT_PASS;
    UINT32  hwkey;

	if(win_popup_type!= WIN_POPUP_TYPE_SMSG)
	{
		switch(vkey)
		{
		case V_KEY_MENU:
		case V_KEY_EXIT:
			win_popup_choice = WIN_POP_CHOICE_NULL;
			act = VACT_CLOSE;
			break;
		case V_KEY_LEFT:
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
				 if(win_popup_type == WIN_POPUP_TYPE_INPUTPIN)
				 {
				 	act = VACT_TVPIN_DEL_ONE_CHAR;
				 }
				 else
				 {
			act = VACT_CURSOR_LEFT;
				 }
#else			
			act = VACT_CURSOR_LEFT;
#endif
			break;
		case V_KEY_RIGHT:
			act = VACT_CURSOR_RIGHT;
			break;            
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))	
			case V_KEY_0:
			case V_KEY_1:
			case V_KEY_2:
			case V_KEY_3:
			case V_KEY_4:
			case V_KEY_5:
			case V_KEY_6:
			case V_KEY_7:
			case V_KEY_8:
			case V_KEY_9:
				
				act = VACT_NUM_0 + ( vkey - V_KEY_0 );
				break;
#endif					
			
		default:
			break;
		}
	}
	else
	{
     #if	defined( SUPPORT_CAS9) ||defined(SUPPORT_CAS7)
        if(menu_stack_get(0) == (POBJECT_HEAD)&g_win_mainmenu)
        {
            if((vkey == V_KEY_EXIT)||(vkey == V_KEY_MENU)||(vkey == V_KEY_LEFT)||(vkey == V_KEY_UP)||(vkey == V_KEY_DOWN))
    		{  
                /*
                if((vkey!=V_KEY_EXIT)||)
    			{	ap_vk_to_hk(0, vkey, &hwkey);
    				ap_send_msg(CTRL_MSG_SUBTYPE_KEY, hwkey, FALSE);//if exit key got,repatch the messages again
    			}*/
                cas_pop_stop_timer();
    			clean_mmi_cur_msg();
                clean_mmi_msg(1,1);
    			act = VACT_CLOSE;

            }
        }
        else
        {
            if((vkey == V_KEY_EXIT)||(vkey == V_KEY_UP)||(vkey == V_KEY_DOWN)||(vkey == V_KEY_MENU))
    		{  
                cas_pop_stop_timer();
    			clean_mmi_cur_msg();
                clean_mmi_msg(1,1);
    			act = VACT_CLOSE;
                if(vkey!=V_KEY_EXIT)
    			{	
                    ap_vk_to_hk(0, vkey, &hwkey);
    				ap_send_msg(CTRL_MSG_SUBTYPE_KEY, hwkey, FALSE);//if exit key got,repatch the messages again
                }
            }
        }
        #else
        if((vkey == V_KEY_EXIT)||(vkey == V_KEY_MENU))
		{
			act = VACT_CLOSE;

        }
        #endif
	}
	return act;
}

static PRESULT popup_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))

	UINT8 unact, input, save;
	UINT8 id = OSD_GetObjID(pObj);
	UINT16 vkey;
	POBJECT_HEAD newMenu;
	UINT8 i;
	UINT8 ret;
	
	vkey = (UINT16)param1;

	if(vkey==V_KEY_EXIT||vkey ==  V_KEY_MENU)
	{
		win_compopup_set_enter_wrong_pin_mmi_status(FALSE);
		return PROC_LEAVE;
	}
#endif	

	switch(event)
	{
	case EVN_PRE_DRAW:
		if(set_change_focus != -1)
		{
			OSD_SetContainerFocus((PCONTAINER)pObj, set_change_focus);
		}
		break;
	case EVN_PRE_OPEN:
		break;
	case EVN_PRE_CLOSE:
		/* Make OSD not flickering */
		if(menu_stack_get_top())
		{
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
			if(	(!api_abel_mmi_check_mainmenu_on_top())&&
				(api_abel_osd_get_cas_msg_num()==0))
#endif			
				{
			*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;
		}
			}
			break;

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
		case EVN_POST_CLOSE:	
			on_event_cmsg_popup_is_closed();
			win_compopup_reset_target_tvpin_value();
		break;

		case EVN_UNKNOWN_ACTION:
			unact = ( VACTION ) ( param1 >> 16 ) ;
			if(g_tvpin_confirm_mode)
			{
				if ( unact <= VACT_NUM_9 )
				{
					if(g_input_tvpin_pos<=3)
					{
						g_input_tvpin_chars[g_input_tvpin_pos]=unact-VACT_NUM_0+'0';
						g_input_tvpin_pos++;
						_update_popup_item_tvpin_display();
					}
					
					if(g_input_tvpin_pos == 4)
					{
						if(api_abel_mmi_check_mainmenu_on_top())
						{
							if(menu_stack_get_top()==&g_win_change_tv_pin)
							{
								ABEL_PRINTF("-[UI] Change TV PIN\n");

								for(i=0; i<8; i++)
								{
									ABEL_PRINTF("g_target_tvpin_for_confirm_ui[%d]=%c (%d)\n", i, g_target_tvpin_for_confirm_ui[i], g_target_tvpin_for_confirm_ui[i]);
								}

								if(kms_change_pin(g_input_tvpin_chars, g_target_tvpin_for_confirm_ui)==SUCCESS)					
								{
									g_tvpin_confirm_proc_exec_result=TRUE;
									ABEL_PRINTF("kms_change_pin()::SUCCESS\n");
									ret = SUCCESS;
								}
								else
								{
									g_tvpin_confirm_proc_exec_result=FALSE;
									ABEL_PRINTF("kms_change_pin()::FAIL\n");
									ret = !SUCCESS;
								}
							}
							else if(menu_stack_get_top()==&g_win_parental_control)
							{
								ABEL_PRINTF("-[UI] Parental Control\n");
							
								if(parental_control(g_input_tvpin_chars, g_target_parental_control_level_for_confirm_ui)==SUCCESS)
								{
									g_tvpin_confirm_proc_exec_result=TRUE;
									ABEL_PRINTF("kms_change_pin()::SUCCESS\n");
									ret = SUCCESS;
								}
								else
								{
									g_tvpin_confirm_proc_exec_result=FALSE;
									ABEL_PRINTF("kms_change_pin()::FAIL\n");
									ret = !SUCCESS;
								}					
							}
							else
							{
								ABEL_PRINTF("-[UI] Unknown (should be preview menu)\n");
						
								if(user_confirm(TV_PIN, g_input_tvpin_chars)==SUCCESS)					
								{
									g_tvpin_confirm_proc_exec_result=TRUE;
									ABEL_PRINTF("kms_change_pin()::SUCCESS\n");							
									ret = SUCCESS;
								}
								else
								{
									g_tvpin_confirm_proc_exec_result=FALSE;
									ABEL_PRINTF("kms_change_pin()::FAIL\n");
									ret = !SUCCESS;
								}
							}
						}
						else
						{
							ABEL_PRINTF("-[UI] Full Screen Display\n");
							if(user_confirm(TV_PIN, g_input_tvpin_chars)==SUCCESS)					
							{
								g_tvpin_confirm_proc_exec_result=TRUE;
								ABEL_PRINTF("user_confirm()::SUCCESS\n");
								ret = SUCCESS;
							}
							else
							{
								g_tvpin_confirm_proc_exec_result=FALSE;
								ABEL_PRINTF("user_confirm()::FAIL\n");
								ret = !SUCCESS;
							}
						}

						if(ret != SUCCESS)
						{
							g_input_tvpin_pos = 0;
							_update_popup_item_tvpin_display();
							return PROC_LOOP;
						}
						
						return PROC_LEAVE;
					}
					
				}
				else if(unact == VACT_TVPIN_DEL_ONE_CHAR)
				{					
					if(g_input_tvpin_pos>0)
					{
						g_input_tvpin_pos--;
						g_input_tvpin_chars[g_input_tvpin_pos]=0;
						_update_popup_item_tvpin_display();
					}
				}
			}
			return PROC_LOOP;
		break;
#endif		
			
	}

	
	return PROC_PASS;
}
/*******************************************************************************
*	BUTTON --- proc
********************************************************************************/

static VACTION popup_btn_text_keymap(POBJECT_HEAD pObj, UINT32 vkey)
{
	VACTION act = VACT_PASS;

	if(vkey==V_KEY_ENTER)
		act = VACT_ENTER;

	return act;
}


static PRESULT popup_btn_text_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	VACTION act;
	UINT16  vkey;
	UINT8   id;

	if(event == EVN_UNKNOWN_ACTION)
	{
		id      = pObj->bID;   
		act     = (UINT8)(param1>>16);
		vkey    = (UINT16)param1;

		if(act == VACT_ENTER)
		{
			if(id==POPUP_YES_ID)
				win_popup_choice = WIN_POP_CHOICE_YES;
			else if(id==POPUP_NO_ID)
				win_popup_choice = WIN_POP_CHOICE_NO;
			else if(id==POPUP_CANCEL_ID)
				win_popup_choice = WIN_POP_CHOICE_CANCEL;
			else
				win_popup_choice = WIN_POP_CHOICE_NULL;

			return PROC_LEAVE;
		}
	}

	return PROC_PASS;
}
/*********************************************************************************
*
*	Window  open,proc
*
**********************************************************************************/
static BOOL win_popup_opened  = FALSE;
static BOOL cur_popup_type  = WIN_POPUP_TYPE_SMSG;
static void win_popup_Init()
{
    
    POBJECT_HEAD pObj;
	CONTAINER* con;

	
    con = &g_win_popup;  
    pObj = (POBJECT_HEAD)&g_win_popup;
    OSD_SetColor(con,POPUPWIN_IDX_SD,POPUPWIN_IDX_SD,0,0);
    pObj = (POBJECT_HEAD)&win_popup_title_txt;
    OSD_SetColor(pObj,POPUP_TITLE_SH_IDX_SD,POPUP_TITLE_SH_IDX_SD,0,0);
    pObj = (POBJECT_HEAD)&win_popup_msg_mbx;
    OSD_SetColor(pObj,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD);
    pObj = (POBJECT_HEAD)&win_popup_yes;
    OSD_SetColor(pObj,POPUPSH_IDX_SD, POPUPHL_IDX_SD, 0,0); 
    pObj = (POBJECT_HEAD)&win_popup_no;        
    OSD_SetColor(pObj,POPUPSH_IDX_SD, POPUPHL_IDX_SD, 0,0); 
    pObj = (POBJECT_HEAD)&win_popup_cancel;
    OSD_SetColor(pObj,POPUPSH_IDX_SD, POPUPHL_IDX_SD, 0,0);         
	OSD_TrackObject((POBJECT_HEAD)con, C_UPDATE_ALL|C_DRAW_SIGN_EVN_FLG);
	win_popup_opened = TRUE;
}

void win_msg_popup_open()
{
#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9))
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_msg_popup_smsg_open();
		return;
	}
#endif

#ifdef TRUE_COLOR_HD_OSD

	if(GetSignalStatausShow())
		ShowSignalStatusOSDOnOff(0);
	if(menu_stack_get_top() == NULL)
	{
		if(win_popup_opened)
		{
			win_msg_popup_close();
		}
		win_popup_Init();
	}
#else
	lpVSCR apvscr;
	
	apvscr = OSD_GetTaskVscr(osal_task_get_current_id());
	OSD_UpdateVscr(apvscr);/*Update layer1 vscr*/
	OSD_SetDeviceHandle((HANDLE)g_osd_dev2);/*Switch to layer2 device*/
	api_osd_mode_change(OSD_MSG_POPUP);
    OSDDrv_ShowOnOff((HANDLE)g_osd_dev2,OSDDRV_ON);
	api_set_deo_layer(1);

	win_popup_Init();
	OSD_UpdateVscr(apvscr);
	OSD_SetDeviceHandle((HANDLE)g_osd_dev);
#endif	
}

void win_compopup_close(void)
{
	OBJECT_HEAD* obj;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_close();
		cur_popup_type = !WIN_POPUP_TYPE_SMSG;
		return;
	}
	#endif
	obj= (OBJECT_HEAD*)&g_win_popup;
	OSD_ObjClose(obj,C_CLOSE_CLRBACK_FLG);
	win_popup_opened = FALSE;
	
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
	on_event_able_mmi_close();
#endif		
}

void win_compopup_refresh(void)
{
#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_refresh();
		return;
	}
#endif	
	if(win_popup_opened)
	{
		OSD_DrawObject((OBJECT_HEAD*)&g_win_popup, C_UPDATE_ALL);
	}
}

void win_msg_popup_close()
{
#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_msg_popup_smsg_close();
		cur_popup_type = !WIN_POPUP_TYPE_SMSG;
		return;
	}
#endif

#ifdef TRUE_COLOR_HD_OSD
	if(win_popup_opened)
		win_compopup_close();
#else
	lpVSCR apvscr;
	
	apvscr = OSD_GetTaskVscr(osal_task_get_current_id());
	OSD_UpdateVscr(apvscr);/*Update layer1 vscr*/
	OSD_SetDeviceHandle((HANDLE)g_osd_dev2);/*Switch to layer2 device*/
	api_osd_mode_change(OSD_MSG_POPUP);
	
	win_compopup_close();
	OSD_UpdateVscr(apvscr);
	api_set_deo_layer(0);
    OSDDrv_ShowOnOff((HANDLE)g_osd_dev2,OSDDRV_OFF);
	OSD_SetDeviceHandle((HANDLE)g_osd_dev);
#endif	
}

BOOL win_msg_popup_opend()
{
	return win_popup_opened;
}

/*********************************************************************************
*
*	exported APIs
*
**********************************************************************************/
static UINT8 win_com_popup_frame_set;
void win_compopup_init(win_popup_type_t type)
{
	OBJECT_HEAD* obj;
	UINT8 i,btn_num;
	UINT16 btn_left;
	TEXT_FIELD*  txt;
	CONTAINER* win;
	POBJECT_HEAD pObjNext;
	#ifdef _INVW_JUICE
		if(menu_stack_get_top() == NULL)
				ap_osd_int_restore();
	#endif
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	cur_popup_type = type;
	if(cur_popup_type==WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_init(type);
		return;
	}
	#endif
	win = &g_win_popup;
	msg_str[0] = 0x0000;

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
	on_event_win_compopup_init();
	g_input_tvpin_pos=0;
	g_tvpin_confirm_mode				=	FALSE;
	g_tvpin_confirm_proc_exec_result	=	FALSE;
	memset(g_input_tvpin_chars, 0xFF, 8);	
#endif

	
	win_popup_type = type;
	win_popup_type = (type<=WIN_POPUP_TYPE_OKNOCANCLE) ?  type : WIN_POPUP_TYPE_SMSG;    	
	win_popup_choice = WIN_POP_CHOICE_NULL;//modify for adding welcome page when only open dvbt 2011 10 17
	cnt_poptime = 0;//modify for adding welcome page when only open dvbt 2011 10 17
	popup_mtxt_content_timer.text.pString   = NULL;
	win_popup_choice = WIN_POP_CHOICE_NULL;
	btn_num = win_popup_btm_num[win_popup_type];

 
	OSD_SetContainerFocus(win, 1);
	win->pNextInCntn = (OBJECT_HEAD*)&win_popup_msg_mbx;

	/* Link Window's objects */
	obj = (OBJECT_HEAD*)&win_popup_msg_mbx; 
	pObjNext = (POBJECT_HEAD)&win_popup_msg_mbx_timer;
	OSD_SetObjpNext(obj, pObjNext);
	
	obj = (OBJECT_HEAD*)&win_popup_msg_mbx_timer;    
	pObjNext = (btn_num<=0) ? NULL : (POBJECT_HEAD)win_popup_btm_objs[0];
	OSD_SetObjpNext(obj, pObjNext);

	obj = win_popup_btm_objs[0];
	pObjNext = (btn_num<=1) ? NULL : (POBJECT_HEAD)win_popup_btm_objs[1];
	OSD_SetObjpNext(obj, pObjNext);

	txt = (TEXT_FIELD*)win_popup_btm_objs[0];
	obj = win_popup_btm_objs[1];
	pObjNext = (btn_num<=2) ? NULL : (POBJECT_HEAD)win_popup_btm_objs[2];
	OSD_SetObjpNext(obj, pObjNext);

	if(btn_num==1)
		OSD_SetID(win_popup_btm_objs[0], 1, 1, 1, 1, 1);
	else if(btn_num==2)
	{
		OSD_SetID(win_popup_btm_objs[0], 1, 2, 2, 1, 1);
		OSD_SetID(win_popup_btm_objs[1], 2, 1, 1, 2, 2);
	}
	else if(btn_num==3)
	{
		OSD_SetID(win_popup_btm_objs[0], 1, 3, 2, 1, 1);
		OSD_SetID(win_popup_btm_objs[1], 2, 1, 3, 2, 2);
		OSD_SetID(win_popup_btm_objs[2], 3, 2, 1, 3, 3);
	}

	for(i=0;i<3;i++)
	{
		txt = (TEXT_FIELD*)win_popup_btm_objs[i];
		txt->pString = NULL;
		OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)btn_str_ids[i]);
	}
	win_com_popup_frame_set = 0;
	win_compopup_set_frame(W_MSG_L,W_MSG_T,W_MSG_W,W_MSG_H); 
 	win_com_popup_frame_set = 1;
	set_change_focus = -1;
}

void win_compopup_set_title(char* str,char* unistr,UINT16 strID)
{
	TEXT_FIELD* txt;
	CONTAINER* win = &g_win_popup;
#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7)  || defined( SUPPORT_CAS9))
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_set_title(str,unistr,strID);
		return;
	}
	#endif
	txt = &win_popup_title_txt;
	OSD_SetTextFieldStrPoint(txt, msg_title);
	if(str!=NULL)
		OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)str);
	else if(unistr!=NULL)
		OSD_SetTextFieldContent(txt, STRING_UNICODE, (UINT32)unistr);
	else
	{
		if(strID != 0)
		{
			OSD_SetTextFieldStrPoint(txt, NULL);
			OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)strID);
		}
		else
		{
		win->pNextInCntn = (OBJECT_HEAD*)&win_popup_msg_mbx;
		return;
		}
	}

	OSD_SetContainerNextObj(win,txt);
}

 void win_compopup_set_msg(char* str,char* unistr,UINT16 strID)
{
	UINT8 i,btn_num,lines;
	UINT8* pstr;
	POBJECT_HEAD txt = (POBJECT_HEAD)&win_popup_msg_mbx;
	UINT16 totalw,mw,mh;
	UINT16 l,t,w,h;
	UINT8	title_flag;
	CONTAINER* win = &g_win_popup;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_set_msg(str,unistr,strID);
		return;
	}
	#endif
	title_flag = (win->pNextInCntn == txt)? 0 : 1;

	popup_mtxt_content.bTextType = STRING_UNICODE;
	popup_mtxt_content.text.pString   = msg_str;
	
	if(str!=NULL)
		ComAscStr2Uni(str, msg_str);
	else if(unistr!=NULL)
		ComUniStrCopyChar( (UINT8*)msg_str,unistr);
	else
	{
		popup_mtxt_content.bTextType        = STRING_ID;
		popup_mtxt_content.text.wStringID   = strID;
	}

	btn_num = win_popup_btm_num[win_popup_type];
	if(str != NULL || unistr!= NULL)
		pstr = (UINT8*)msg_str;
	else
		pstr = OSD_GetUnicodeString(strID);
	totalw = OSD_MultiFontLibStrMaxHW(pstr,txt->bFont,&mw,&mh,0);
	totalw += MSG_LOFFSET*2;


	if(btn_num <= 1)
		w = MIN_WIDHT_1ORNOBTN;
	else
		w = MIN_WIDTH_2BTN;

CHECK_LINES:
	if((totalw + w - 1)%w != 0)
		lines = (totalw + w - 1) / w + 1;
	else
	lines = (totalw + w - 1) / w;
	if(lines <= 1)
		lines = 1;
	else
	{
		if(w != MAX_LEN_ONE_LINE)
		{
			w = MAX_LEN_ONE_LINE;
			goto CHECK_LINES;
		}
	}

	if(mh < DEFAULT_FONT_H)
		mh = DEFAULT_FONT_H; 
	h =  mh * lines + (lines  - 1)*4 ;
	if(btn_num == 0)
		h += MSG_VOFFSET_NOBTN*2;
	else
		h += (MSG_VOFFSET_WITHBTN + MSG_BUTTON_TOPOFFSET + MSG_BUTTON_BOTOFFSET + BTM_H);

	if(title_flag)
		h += TITLE_H + 10;
#if	((defined(SUPPORT_CAS9)||defined(SUPPORT_CAS7) || defined(SUPPORT_CAS_A)) && (!defined SD_UI))
        l = 290;
        t = 250;
    #else
        l = (OSD_MAX_WIDTH - w)>>1;
    	t = (OSD_MAX_HEIGHT - h)>>1;
    	t = (t + 1) /2 * 2; 
    #endif
	
	win_compopup_set_frame(l,t,w,h);
}


void win_compopup_set_msg_ext(char* str,char* unistr,UINT16 strID)
{
	UINT8* pstr;
	POBJECT_HEAD txt = (POBJECT_HEAD)&win_popup_msg_mbx;
	UINT8	title_flag;
	CONTAINER* win = &g_win_popup;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7)  || defined( SUPPORT_CAS9))
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_set_msg_ext(str,unistr,strID);
		return;
	}
	#endif
	popup_mtxt_content.bTextType = STRING_UNICODE;
	popup_mtxt_content.text.pString   = msg_str;

	if(str!=NULL)
		ComStr2UniStrExt(msg_str, str, POPUP_MAX_MSG_LEN-1);
	else if(unistr!=NULL)
		ComUniStrCopyCharN((UINT8*)msg_str, unistr, POPUP_MAX_MSG_LEN-1);
	else
	{
		popup_mtxt_content.bTextType        = STRING_ID;
		popup_mtxt_content.text.wStringID   = strID;
	}

	if(str != NULL || unistr!= NULL)
		pstr = (UINT8*)msg_str;
	else
		pstr = OSD_GetUnicodeString(strID);
}

void win_compopup_set_btnstr(UINT32 btn_idx, UINT16 str_id)
{
	TEXT_FIELD* txt;

	if(btn_idx < 3)
	{
		txt = (TEXT_FIELD*)win_popup_btm_objs[btn_idx];
		OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)str_id);
	}
}

void win_compopup_set_btnstr_ext(UINT32 btn_idx, char* str)
{
	TEXT_FIELD* txt;
	char temp_str[10];

	if(btn_idx < 3)
	{
		txt = (TEXT_FIELD*)win_popup_btm_objs[btn_idx];
		sprintf(temp_str,"%s", str);
		txt->pString = msg_btn_str[btn_idx];
		OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)temp_str);
	}
}

void win_compopup_set_frame(UINT16 x,UINT16 y,UINT16 w,UINT16 h)
{
	OBJECT_HEAD* obj;
	UINT8 i,btn_num;
	UINT16 btn_left,btn_top,btm_width;
	UINT16 topoffset,botoffset;

	UINT16 msgwidth,wMaxHeight,wMaxWidth;
	OBJECT_HEAD *objmbx;
	OBJECT_HEAD *objmbx_timer;//modify for adding welcome page when only open dvbt 2011 10 17
	MULTI_TEXT*  mbx;
	MULTI_TEXT*  mbx_timer;//modify for adding welcome page when only open dvbt 2011 10 17
	UINT8	title_flag;
	CONTAINER* win = &g_win_popup;
	TEXT_FIELD* txt_title = &win_popup_title_txt;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_set_frame(x,y,w,h);
		return;
	}
	#endif
#ifdef SD_UI
	if(x+w>=OSD_MAX_WIDTH)
		w = OSD_MAX_WIDTH-x-80;
	if(y+h>=OSD_MAX_HEIGHT)
		h = OSD_MAX_HEIGHT-y-80;
#endif

	title_flag = (OSD_GetContainerNextObj(win) == (POBJECT_HEAD)txt_title)? 1 : 0;
	if(title_flag)
		OSD_SetRect(&txt_title->head.frame, x + 10, y + 6, w - 20, TITLE_H);
	
	obj = (OBJECT_HEAD*)&g_win_popup;    
	OSD_SetRect(&obj->frame, x,y,w,h);
	btn_num = win_popup_btm_num[win_popup_type];
	objmbx = (OBJECT_HEAD*)&win_popup_msg_mbx;
	objmbx_timer = (OBJECT_HEAD*)&win_popup_msg_mbx_timer;//modify for adding welcome page when only open dvbt 2011 10 17
	mbx = &win_popup_msg_mbx;
	mbx_timer = &win_popup_msg_mbx_timer;//modify for adding welcome page when only open dvbt 2011 10 17
	
	if(btn_num==0)
	{
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
		if(win_popup_type == WIN_POPUP_TYPE_INPUTPIN)
		{
			mbx->bAlign = C_ALIGN_LEFT| C_ALIGN_TOP;
		}
		else
		{
		mbx->bAlign = C_ALIGN_CENTER | C_ALIGN_VCENTER;         
		}
#else	
		mbx->bAlign = C_ALIGN_CENTER | C_ALIGN_VCENTER;         
#endif
		topoffset = MSG_VOFFSET_NOBTN;
		if(h - topoffset*2 < DEFAULT_FONT_H)
			topoffset = (h - DEFAULT_FONT_H)/2;

		botoffset = topoffset;
		if(title_flag)
			topoffset += TITLE_H;
	}
	else
	{
		mbx->bAlign = C_ALIGN_CENTER | C_ALIGN_TOP;         
		topoffset = MSG_VOFFSET_WITHBTN;//h/5;
		botoffset = topoffset + BTM_H;
		if(title_flag)
			topoffset += TITLE_H;
	}
	
	OSD_SetObjRect(objmbx,x + MSG_LOFFSET,y + topoffset,w - MSG_LOFFSET*2,h - botoffset - topoffset);
	
	//modify for adding welcome page when only open dvbt 2011 10 17
	timer_rect.uStartX = x + MSG_LOFFSET+5;
	timer_rect.uStartY = y + topoffset;
	timer_rect.uWidth  = w - MSG_LOFFSET*2-10;
	timer_rect.uHeight = h - botoffset - topoffset;
	OSD_SetObjRect(objmbx_timer,x + MSG_LOFFSET+5,y + topoffset+40,w - MSG_LOFFSET*2-10,0);
	//modify end
	mbx->rcText.uWidth  = objmbx->frame.uWidth;
	mbx->rcText.uHeight = objmbx->frame.uHeight;  
	//modify for adding welcome page when only open dvbt 2011 10 17
	mbx_timer->rcText.uWidth  = objmbx->frame.uWidth;
	mbx_timer->rcText.uHeight = 40/*objmbx->frame.uHeight*/;  
	//modify end

	for(i=0;i<btn_num;i++)
	{
		obj = win_popup_btm_objs[i];
		btm_width = BTM_W;

		if(btn_num==1)
			btn_left = x + (w - btm_width)/2;
		else if(btn_num==2)
			btn_left = x + ( w - btm_width*btn_num - BTM_GAP)/2 + (btm_width + BTM_GAP) *i ;
		else 
			btn_left = x +  w - (btm_width + BTM_GAP) * ( btn_num - i) - 40;
		btn_top = h - MSG_BUTTON_BOTOFFSET - BTM_H;
		OSD_SetRect(&obj->frame, btn_left,(y + btn_top),btm_width,BTM_H);
	}

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
	TEXT_FIELD *txt_item_name 	= &win_popup_item_name;
	TEXT_FIELD *txt_item_val 		= &win_popup_item_val;

	if(OSD_GetObjpNext(objmbx)==&win_popup_item_name)
	{	
#if 0	
		// ITEM_NAME_W = 1/2 Width, ITEM_VAL_W = 1/2 Width
		OSD_SetRect(&txt_item_name->head.frame, x + MSG_LOFFSET, y + h - MSG_BUTTON_BOTOFFSET - BTM_H , (w - MSG_LOFFSET * 2)/2, TITLE_H);
		//libc_printf("item_name(x=%d, y=%d, w=%d, h=%d)\n", x + MSG_LOFFSET, y + h - MSG_BUTTON_BOTOFFSET - BTM_H , (w - MSG_LOFFSET * 2)/2, TITLE_H);	

		obj = &win_popup_item_name;
		if(OSD_GetObjpNext(obj)==&win_popup_item_val)
		{
			OSD_SetRect(&txt_item_val->head.frame, x + MSG_LOFFSET+(w - MSG_LOFFSET * 2)/2 - val_Xoffset, y + h - MSG_BUTTON_BOTOFFSET - BTM_H, (w - MSG_LOFFSET * 2)/2, TITLE_H);	
			//libc_printf("item_val(x=%d, y=%d, w=%d, h=%d)\n", x + MSG_LOFFSET+(w - MSG_LOFFSET * 2)/2 - val_Xoffset, y + h - MSG_BUTTON_BOTOFFSET - BTM_H, (w - MSG_LOFFSET * 2)/2, TITLE_H);	
		}
#else
		// ITEM_NAME_W = 2/3 Width, ITEM_VAL_W = 1/3 Width
		OSD_SetRect(&txt_item_name->head.frame, x + MSG_LOFFSET, y + h - MSG_BUTTON_BOTOFFSET - BTM_H - BTM_GAP , ((w - MSG_LOFFSET * 2)*2)/3, ITEM_TXT_H);
		//libc_printf("item_name(x=%d, y=%d, w=%d, h=%d)\n", x + MSG_LOFFSET, y + h - MSG_BUTTON_BOTOFFSET - BTM_H - BTM_GAP , ((w - MSG_LOFFSET * 2)*2)/3, ITEM_TXT_H);	

		obj = &win_popup_item_name;
		if(OSD_GetObjpNext(obj)==&win_popup_item_val)
		{
			OSD_SetRect(&txt_item_val->head.frame, x + MSG_LOFFSET+ ((w - MSG_LOFFSET * 2)*2)/3 - val_Xoffset, y + h - MSG_BUTTON_BOTOFFSET - BTM_H  - BTM_GAP ,  ((w - MSG_LOFFSET * 2))/3, ITEM_VAL_H);	
			//libc_printf("item_val(x=%d, y=%d, w=%d, h=%d)\n", x + MSG_LOFFSET+ ((w - MSG_LOFFSET * 2)*2)/3 - val_Xoffset, y + h - MSG_BUTTON_BOTOFFSET - BTM_H - BTM_GAP,  ((w - MSG_LOFFSET * 2))/3, ITEM_VAL_H);	
		}
#endif
	}
	else if(OSD_GetObjpNext(objmbx)==&win_popup_item_val)
	{
		OSD_SetRect(&txt_item_val->head.frame, x + MSG_LOFFSET+(w - MSG_LOFFSET * 2)/2, y + h - MSG_BUTTON_BOTOFFSET - BTM_H*2 - BTM_GAP, (w - MSG_LOFFSET * 2)/2, TITLE_H);	
	}
#endif	
	
}

void win_compopup_set_pos(UINT16 x,UINT16 y)
{
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_set_pos(x,y);
		return;
	}
	#endif
	OSD_MoveObject(&g_win_popup, x, y, FALSE);
}

void win_compopup_set_default_choice(win_popup_choice_t choice)
{
	win_popup_choice = choice;
	g_win_popup.FocusObjectID = win_popup_choice ;
}

 win_popup_choice_t win_compopup_open(void)
{
	CONTAINER* con =&g_win_popup;
	PRESULT bResult = PROC_LOOP;
	UINT32 hkey;
	UINT32 msg;
	UINT ct =cnt_poptime;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7)  || defined( SUPPORT_CAS9))
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_open();
		return WIN_POP_CHOICE_NULL;
	}
	#endif

	win_msg_popup_close();
#ifdef CI_PLUS_SUPPORT
    if(IsCIPlus_Menu_Exist())
    {
        return WIN_POP_CHOICE_NULL;
    }
#endif


#if (defined(SUPPORT_VMX_STD) && defined(VMX_PATCH_UI) )//closing windows when show order pin or check pin
	extern UINT8 gOrderPinDialog;
	DWORD start_tick;
	start_tick=osal_get_tick();
#elif defined(SUPPORT_VMX)
	extern UINT8 gOrderPinDialog;
	DWORD start_tick;
	start_tick=osal_get_tick();
#endif

	win_popup_Init();
	if(win_popup_type!=WIN_POPUP_TYPE_SMSG)
	{
        BOOL old_value = ap_enable_key_task_get_key(TRUE);
		while(bResult != PROC_LEAVE)
		{
#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
			if(api_abel_mmi_check_mmi_in_progress()&&(menu_stack_get_top()==(POBJECT_HEAD)&g_win_progname))
			{
				/* process win_progname first, then process abel user confirm mmi :
				     solution: clear g_win_progname first then process user confirm mmi. */
				//libc_printf("<g_win_progname>\n");			
				osal_task_sleep(1500);
				OSD_ClearObject((POBJECT_HEAD)&g_win_progname,C_UPDATE_ALL);
				menu_stack_pop();
			}
#endif
		#if (defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX))
		osm_api_osd_display_window_control_proc();
		#endif

			hkey = ap_get_key_msg();
			if(hkey == INVALID_HK || hkey == INVALID_MSG)
			{
				continue;
			}

			#if defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX)
			if(hkey==CTRL_MSG_SUBTYPE_CMD_OSM_HANDLE_NR_OSM_TIMER)
			{
				//libc_printf("%s-%d Got Msg CTRL_MSG_SUBTYPE_CMD_OSM_HANDLE_NR_OSM_TIMER\n",__FILE__,__LINE__); //vicky20130108#3
				handle_nonremovable_osm_about_timer();			
			}
			else if (hkey==CTRL_MSG_SUBTYPE_CMD_OSM_HANDLE_WATERMARKING_TIMER)
			{
				//libc_printf("%s-%d Got Msg CTRL_MSG_SUBTYPE_CMD_OSM_HANDLE_WATERMARKING_TIMER\n",__FILE__,__LINE__);
				_osm_api_osd_display_window_open();	
			}
			else if (hkey==CTRL_MSG_SUBTYPE_CMD_OSM_WATERMARKING)
			{
				//libc_printf("%s-%d Got Msg CTRL_MSG_SUBTYPE_CMD_OSM_WATERMARKING\n",__FILE__,__LINE__);
				Watermarking(gdwWmDur);			
			}
			#endif
			
			#if defined(SUPPORT_VMX_STD) || defined(SUPPORT_VMX)
				UINT32 vkey;
				ap_hk_to_vk(0, hkey, &vkey);				
			#endif
			
            #if (defined(SUPPORT_VMX_STD)&& defined(VMX_PATCH_UI))
				if((osal_get_tick()-start_tick<600)||(gOrderPinDialog!=TRUE)) //closing windows when show order pin or check pin
				{
					if ((vkey == V_KEY_UP) || (vkey == V_KEY_DOWN) )
						vkey=V_KEY_NULL;
				}
			#elif defined(SUPPORT_VMX)
				if((osal_get_tick()-start_tick<600)||(gOrderPinDialog!=TRUE)) //closing windows when show order pin or check pin
				{
					if ((vkey == V_KEY_UP) || (vkey == V_KEY_DOWN) )
						vkey=V_KEY_NULL;
				}
			#endif
			//modify for adding welcome page when only open dvbt 2011 10 16
			if ((CTRL_MSG_TYPE_POPUP_TIMER == hkey) || (CTRL_MSG_SUBTYPE_CMD_PVR_AUTO_STANDBY_TIMER == hkey))
			{
				if (ct != 0)
				{
					ComAscStr2Uni(win_popup_timer_count[ct-1], msg_timer_str);
				popup_mtxt_content_timer.bTextType = STRING_ANSI;//STRING_UNICODE;
					popup_mtxt_content_timer.text.pString   = msg_timer_str;
				OSD_TrackObject((POBJECT_HEAD)&g_win_popup,C_UPDATE_ALL);
				ct--;
				}
				else
				{
					popup_mtxt_content_timer.text.pString   = NULL;
					OSD_TrackObject((POBJECT_HEAD)&g_win_popup,C_UPDATE_ALL);
				}
				
				if (ct == 0)
				{
					api_stop_timer(&popup_timer_id);
					ap_enable_key_task_get_key(old_value);
					if (CTRL_MSG_TYPE_POPUP_TIMER == hkey)
						return WIN_POP_CHOICE_NO;
					else
						return win_popup_choice;
				}
			}
		#if defined(SUPPORT_VMX_STD)			
			else if(CTRL_MSG_SUBTYPE_CMD_EXIT == hkey || vkey == V_KEY_UP || vkey == V_KEY_DOWN)	//issue: Order not remove when event finish
			{
			    #if defined(VMX_PATCH_UI)
                if(gOrderPinDialog==TRUE)	//closing windows when show order pin or check pin
				{
                #endif
				if (vkey == V_KEY_UP) {
					//libc_printf("channel up\n");					
					change_channel(1);					
				} else if (vkey == V_KEY_DOWN) {
					//libc_printf("channel down\n");					
					change_channel(-1);
				}			
			    #if defined(VMX_PATCH_UI)
                }
                #endif
				ap_enable_key_task_get_key(old_value);
				return WIN_POP_CHOICE_NO;
			}
		#elif defined(SUPPORT_VMX)
			else if(CTRL_MSG_SUBTYPE_CMD_EXIT == hkey || vkey == V_KEY_UP || vkey == V_KEY_DOWN)	//issue: Order not remove when event finish
			{
                if(gOrderPinDialog==TRUE)	//closing windows when show order pin or check pin
				{
					if (vkey == V_KEY_UP) {
						//libc_printf("channel up\n");
						change_channel(1);
					} else if (vkey == V_KEY_DOWN) {
						//libc_printf("channel down\n");
						change_channel(-1);
					}			
                }
				ap_enable_key_task_get_key(old_value);
				return WIN_POP_CHOICE_NO;
			}
		#endif
			else
			{
			bResult = OSD_ObjProc((POBJECT_HEAD)con, (MSG_TYPE_KEY<<16),hkey, 0);//w->head.pProc((OBJECTHEAD *)w, msgcode, 0, save_back_ok);
		}
		}
		api_stop_timer(&popup_timer_id);
		//modify end
        ap_enable_key_task_get_key(old_value);
	}

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
	on_event_able_mmi_close();
#endif

	return win_popup_choice;
}

win_popup_choice_t win_com_popup_open(win_popup_type_t type,char* str,UINT16 strID)
{
	lpVSCR apvscr;
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7)  || defined( SUPPORT_CAS9))
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_com_popup_smsg_open(type,str,strID);
		return WIN_POP_CHOICE_NULL;
	}
	#endif
	win_compopup_init(type);
	win_compopup_set_msg(str,NULL,strID);
	return win_compopup_open();
}

win_popup_choice_t win_compopup_open_ext(UINT8* back_saved)
{
	win_popup_choice_t ret;
    win_popup_type_t  type;
    type = win_popup_type;
  #if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_open_ext();
		return WIN_POP_CHOICE_NULL;
	}
	#endif
	wincom_backup_region(&g_win_popup.head.frame);
	ret = win_compopup_open();
	if(type != WIN_POPUP_TYPE_SMSG)
	{
		*back_saved = 1;
		if(win_popup_opened)
		{
			win_msg_popup_close();
		}
		wincom_restore_region();
	}
	else
	{
		*back_saved = 0;
	}

	return ret;
	}


win_popup_choice_t win_compopup_open_enh(POBJECT_HEAD root_obj, UINT32 draw_mode, UINT32 cmd)
{
	win_popup_choice_t ret;
	struct OSDRect*	frame_ptr;
	struct OSDRect rect;
	CONTAINER* win = &g_win_popup;
	POBJECT_HEAD obj;
	UINT16 x, y;
	BOOL adjust_xy = FALSE;
	
	frame_ptr = &root_obj->frame;
	obj = (POBJECT_HEAD)&g_win_popup;    

	if(obj->frame.uHeight > frame_ptr->uHeight || 
		obj->frame.uWidth > frame_ptr->uWidth)
	{
		return WIN_POP_CHOICE_NULL;
	}

	x = obj->frame.uLeft;
	y = obj->frame.uTop;
	if(obj->frame.uTop < frame_ptr->uTop)
	{
		x = frame_ptr->uTop;
		adjust_xy = TRUE;
	}
	if(obj->frame.uLeft < frame_ptr->uLeft)
	{
		y = frame_ptr->uLeft;
		adjust_xy = TRUE;
	}
	if(adjust_xy)
	{
		win_compopup_set_frame(x,y,obj->frame.uWidth,obj->frame.uHeight);
	}
	
	ret = win_compopup_open();

	if(root_obj)
	{
		if(draw_mode&0x01)
		{
			OSD_TrackObject(root_obj,cmd);
		}
		else if(!(draw_mode&0x01))
		{
			OSD_DrawObject(root_obj,cmd);
		}
	}

	return ret;
}

void win_compopup_smsg_restoreback(void)
{
	#if !(defined( _BUILD_OTA_E_) ||defined( SUPPORT_CAS7) || defined( SUPPORT_CAS9) )
	if(cur_popup_type == WIN_POPUP_TYPE_SMSG)
	{
		win_compopup_smsg_smsg_restoreback();
		return;
	}
	#endif
	if(win_popup_opened)
 	{
 		win_msg_popup_close();
 	}
	wincom_restore_region();
}

/////////////////////////////////////////////
static ID wincom_dish_move_timer_id = OSAL_INVALID_ID;
static BOOL movetime_expire = FALSE;
static void wincom_dish_move_timerhandler()
{
	if(wincom_dish_move_timer_id!= OSAL_INVALID_ID)
		movetime_expire = TRUE;
}



void wincom_dish_move_popup_open(UINT16 pre_sat_id,UINT16 cur_sat_id, UINT32 tuner_idx,UINT8* back_saved)
{
	S_NODE pre_s_node, cur_s_node;
	UINT16 str[100];
	UINT16 nStrLen;

	OSAL_ER	retVal;
	BOOL bValidKey;
	CONTAINER* win;
	UINT8 save_back;
	ControlMsg_t	controlMsg;
	UINT32 msgSize;
	UINT32 hkey = INVALID_HK;
	UINT32 vkey = V_KEY_NULL;
	UINT8 signal_lock = 0;
	UINT32 wait_time = 0;
	Usals_Local local;
	double sat_orbit;
	double local_longitude;
	double local_latitude;    
	double xx_angle;
	INT32 preret,curret;
	UINT32 show_pre_sat = 0;
	struct nim_device* nim_dev;
#ifdef NEW_DEMO_FRAME
	struct ft_antenna antenna_pre, antenna_cur;
#else
	struct cc_antenna_info antenna_pre,antenna_cur;
#endif
	
	/* Signal detect */
	if(tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

    if(FRONTEND_TYPE_S != dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM))
        return;
	
	*back_saved = 1;

	if(pre_sat_id==cur_sat_id/* ||pre_sat_pos==INVALID_POS_NUM*/||cur_sat_id== 0xFFFF)	
		return;
   
	//preret = get_sat_at(pre_sat_pos, VIEW_ALL,&pre_s_node);
	//curret = get_sat_at(cur_sat_pos, VIEW_ALL,&cur_s_node);
	preret = get_sat_by_id(pre_sat_id,&pre_s_node);
	curret = get_sat_by_id(cur_sat_id,&cur_s_node);
	if(curret != SUCCESS)
		return;

//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
  #ifdef DVBS_SUPPORT

    #ifdef NEW_DEMO_FRAME
	sat2antenna(&cur_s_node, &antenna_cur);
    #else
	sat2antenna_ext(&cur_s_node, &antenna_cur, tuner_idx);
    #endif
  #endif
//#endif  


    if(antenna_cur.positioner_type==0)
        return;
    else if(antenna_cur.positioner_type==1)  /* DisEqc 1.2 */
    {
        if(antenna_cur.position == 0)   /* NULL*/ 
            return;
    }
    else if(antenna_cur.positioner_type == 2)   /* DisEqc 1.3 */ 
    {
        sys_data_get_local_position(&local);
        
        sat_orbit = api_usals_get_sat_orbit(cur_s_node.sat_orbit);        
        local_longitude = api_usals_local_word2double(local.local_longitude);            
        local_latitude  = api_usals_local_word2double(local.local_latitude);        
        xx_angle = api_usals_target_formula_calcuate(sat_orbit, local_longitude, local_latitude);
        if(xx_angle > 90)
        {
            win_compopup_init(WIN_POPUP_TYPE_SMSG);
            win_compopup_set_frame(150, 120, 300, 80);
            win_compopup_set_msg(NULL, NULL, RS_MSG_OUT_RANGE);
            win_compopup_open_ext(&save_back);
            *back_saved = save_back;
            osal_task_sleep(1000);
            win_compopup_smsg_restoreback();            
            return;
        }
    }
    else
        return;
    
    if(pre_sat_id != 0xFFFF && preret==SUCCESS)
    {

//#if (SYS_PROJECT_FE == PROJECT_FE_DVBS || SYS_PROJECT_FE == PROJECT_FE_DVBS2)
  #ifdef DVBS_SUPPORT
    #ifdef NEW_DEMO_FRAME
	  sat2antenna(&pre_s_node, &antenna_pre);
    #else
      sat2antenna_ext(&pre_s_node, &antenna_pre, tuner_idx);
    #endif
  #endif
//#endif  
		
        if(antenna_pre.positioner_type == 1)  /* DisEqc 1.2 */
        {
            if(antenna_cur.positioner_type == 1
                && antenna_pre.position == antenna_cur.position)
                return;
            else if(antenna_pre.position != 0)
                show_pre_sat = 1;
        }
        else if( antenna_pre.positioner_type == 2)   /* DisEqc 1.3 */ 
        {
            if(antenna_cur.positioner_type == 2
                && pre_s_node.sat_orbit == cur_s_node.sat_orbit)
                return;
            else
                show_pre_sat = 1;            
        }
   }
   
    
    BOOL old_value = ap_enable_key_task_get_key(TRUE);
    
	MEMSET(str, 0, sizeof(str));
    
#ifndef DB_USE_UNICODE_STRING    
	STRCPY("Moving dish,please wait...      ",str);
	nStrLen = STRLEN(str);
    if(pre_sat_id != 0xFFFF && pre_s_node.positioner_type!=0)
    {
    	STRCPY(&str[nStrLen], pre_s_node.sat_name);
    	nStrLen = STRLEN(str);
    }
	STRCPY(&str[nStrLen], "-->>");
	nStrLen = STRLEN(str);
	STRCPY(&str[nStrLen], cur_s_node.sat_name);
#else
    ComAscStr2Uni("Moving dish,please wait...      ",str);
    nStrLen = ComUniStrLen(str);
    if(show_pre_sat)
    {
        ComUniStrCopyChar((UINT8*)&str[nStrLen], pre_s_node.sat_name);
        nStrLen = ComUniStrLen(str);
    }
	if(!show_pre_sat)
		ComAscStr2Uni("\n-->>",&str[nStrLen]);
	else
		ComAscStr2Uni("-->>",&str[nStrLen]);
    nStrLen = ComUniStrLen(str);
    ComUniStrCopyChar((UINT8*)&str[nStrLen], cur_s_node.sat_name);
    nStrLen = ComUniStrLen(str);
#endif
	
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_frame(100, 140,400, 80); 
    
#ifndef DB_USE_UNICODE_STRING
    win_compopup_set_msg("Moving dish...", NULL,0);
#else
    win_compopup_set_msg("Moving dish...", NULL,0);
#endif
     
	//win_popup_Init();
	movetime_expire = FALSE;
	wincom_dish_move_timer_id = api_start_timer("dish move", 20000, wincom_dish_move_timerhandler);

	win_compopup_open_ext(back_saved);
    
	while(!( ((vkey== V_KEY_EXIT) || (vkey== V_KEY_MENU) || (vkey == V_KEY_RIGHT) || (vkey == V_KEY_LEFT))	\
		||signal_lock!=0 ||movetime_expire))
	{
		hkey = ap_get_key_msg();    /* wait for 50ms */  
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
			continue;  
		ap_hk_to_vk(0,hkey, &vkey);
		
		if(cur_tuner_idx == 0)
			nim_dev = g_nim_dev;
		else
			nim_dev = g_nim_dev2;	
		
		if(wait_time>=20)
			nim_get_lock(nim_dev, &signal_lock);
		wait_time++;
  	}

	if((vkey== V_KEY_RIGHT) || (vkey == V_KEY_LEFT))
		ap_send_msg(CTRL_MSG_SUBTYPE_KEY, hkey, FALSE);

	api_stop_timer(&wincom_dish_move_timer_id);
	win_compopup_smsg_restoreback();

	*back_saved = save_back;
    ap_enable_key_task_get_key(old_value);
}
void win_compopup_set_mpbtn(BOOL bSet)
{
	UINT8 i;
	TEXT_FIELD *txt;

	for(i=0;i<3;i++)
	{
		txt = (TEXT_FIELD*)win_popup_btm_objs[i];
		if(bSet)
			OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)mp_btn_ids[i]);
		else
			OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)btn_str_ids[i]);
	}
}
//modify for adding welcome page when only open dvbt2011 10 17
// unit of time as ms, unit of TimeOut as Sec.
void win_compopup_set_timer(UINT32 time, UINT32 msgtype, UINT16 TimeOut)
{

	if(CTRL_MSG_TYPE_POPUP_TIMER==msgtype)
	{
		OSD_SetObjRect((OBJECT_HEAD*)&win_popup_msg_mbx_timer,timer_rect.uStartX,timer_rect.uStartY+40,timer_rect.uWidth,40);
		popup_timer_id = api_start_cycletimer(NULL, time, win_compopup_timer_countdown);
	}
#ifdef PVR_AUTO_STANDBY_SUPPORT
	else if(CTRL_MSG_SUBTYPE_CMD_PVR_AUTO_STANDBY_TIMER==msgtype)
	{
		//win_compopup_set_frame(timer_rect.uStartX, timer_rect.uStartY, timer_rect.uWidth, timer_rect.uHeight+100);
		OSD_SetObjRect((OBJECT_HEAD*)&win_popup_msg_mbx_timer,timer_rect.uStartX,timer_rect.uStartY+40,timer_rect.uWidth,40);
		popup_timer_id = api_start_cycletimer(NULL, time, win_compopup_pvr_auto_standby_timer_countdown);
	}
#endif
	cnt_poptime=TimeOut;

}
#ifdef PVR_AUTO_STANDBY_SUPPORT
void win_compopup_pvr_auto_standby_timer_countdown()
{	
	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_PVR_AUTO_STANDBY_TIMER,CTRL_MSG_SUBTYPE_CMD_PVR_AUTO_STANDBY_TIMER,FALSE);
}
#endif

void win_compopup_timer_countdown()
{	
	ap_send_msg(CTRL_MSG_TYPE_POPUP_TIMER,CTRL_MSG_TYPE_POPUP_TIMER,FALSE);
}

void win_compopup_auto_standby_timer_countdown()
{	
	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_AUTO_STANDBY, CTRL_MSG_SUBTYPE_CMD_AUTO_STANDBY_TIMER, FALSE);
}

void win_compopup_set_auto_standby_timer(unsigned time)
{
	win_compopup_set_frame(timer_rect.uStartX, timer_rect.uStartY, timer_rect.uWidth, timer_rect.uHeight+100);
#ifdef HDOSD
	OSD_SetObjRect((OBJECT_HEAD*)&win_popup_msg_mbx_timer,timer_rect.uStartX,timer_rect.uStartY+40,timer_rect.uWidth,40);
#else
	OSD_SetObjRect((OBJECT_HEAD*)&win_popup_msg_mbx_timer,timer_rect.uStartX,timer_rect.uStartY+25,timer_rect.uWidth,28);
#endif
	popup_timer_id = api_start_cycletimer(NULL, time, win_compopup_auto_standby_timer_countdown);
}
//modify end
void win_popup_set_focus(INT8 id)
{
	set_change_focus = id;
}

static void send_key_timer_handler(UINT32 vkey)
{
    UINT32 hkey;
    ap_vk_to_hk(0, vkey, &hkey);
    ap_send_msg(CTRL_MSG_SUBTYPE_KEY, hkey, TRUE);
}

ID win_compopup_start_send_key_timer(UINT32 vkey, UINT32 interval, UINT16 timer_type)
{
	OSAL_T_CTIM  t_dalm;
	ID			 timer_id;
    SYSTEM_DATA *p_sys_data = sys_data_get();
	
    if (0xffffffff == interval)
        interval = p_sys_data->osd_set.time_out * 1000;

	MEMSET(&t_dalm, 0, sizeof(OSAL_T_CTIM));
	t_dalm.callback = send_key_timer_handler;
	t_dalm.type = timer_type;//TIMER_CYCLIC;
	t_dalm.time = interval;
	t_dalm.param = vkey;
	
	if ((timer_id = osal_timer_create(&t_dalm)) == OSAL_INVALID_ID)
	{
		return OSAL_INVALID_ID;
	}

	osal_timer_activate(timer_id, 1);
	return timer_id;
}

#if defined(SUPPORT_CAS7) ||defined(SUPPORT_CAS9)
void win_mmicompopup_set_msg(char* str,char* unistr,UINT16 strID)
{
	UINT8 i,btn_num,lines;
	UINT8* pstr;
	POBJECT_HEAD txt = (POBJECT_HEAD)&win_popup_msg_mbx;
	UINT16 totalw,mw,mh;
	UINT16 l,t,w,h;
	UINT8	title_flag;
	CONTAINER* win = &g_win_popup;

	title_flag = (win->pNextInCntn == txt)? 0 : 1;

	popup_mtxt_content.bTextType = STRING_UNICODE;
	popup_mtxt_content.text.pString   = msg_str;
	
	if(str!=NULL)
		ComAscStr2Uni(str, msg_str);
	else if(unistr!=NULL)
		ComUniStrCopyChar( (UINT8*)msg_str,unistr);
	else
	{
		popup_mtxt_content.bTextType        = STRING_ID;
		popup_mtxt_content.text.wStringID   = strID;
	}

	btn_num = win_popup_btm_num[win_popup_type];
	if(str != NULL || unistr!= NULL)
		pstr = (UINT8*)msg_str;
	else
		pstr = OSD_GetUnicodeString(strID);
	totalw = OSD_MultiFontLibStrMaxHW(pstr,txt->bFont,&mw,&mh,0);

	totalw += MSG_LOFFSET*2;

	if(btn_num <= 1)
		w = MIN_WIDHT_1ORNOBTN;
	else
		w = MIN_WIDTH_2BTN;
CHECK_LINES:
	lines = (totalw + w - 1) / w;
	if(lines <= 1)
		lines = 1;
	else
	{
		if(w != MAX_LEN_CONAX_ONE_LINE)
		{
			w = MAX_LEN_CONAX_ONE_LINE;
			goto CHECK_LINES;
		}
	}

	if(mh < DEFAULT_FONT_H)
		mh = DEFAULT_FONT_H; 
	h =  mh * lines + (lines  - 1)*4 ;
	if(btn_num == 0)
		h += MSG_VOFFSET_NOBTN*2;
	else
		h += (MSG_VOFFSET_WITHBTN + MSG_BUTTON_TOPOFFSET + MSG_BUTTON_BOTOFFSET + BTM_H);

	//for jade hd osd
	if(h<144)
		h = 144;

	if(title_flag)
		h += TITLE_H + 10;
	l = (OSD_MAX_WIDTH - w)>>1;
	t = (OSD_MAX_HEIGHT - h)>>1;
	t = (t + 1) /2 * 2 - MSG_VOFFSET_NOBTN;
	win_compopup_set_frame(l,t,w,h);
}
    
BOOL mmipopup_is_closed=TRUE;
OSD_RECT last_msg_rect;
extern BOOL GetChannelScramble(void);
static struct Rect mmi_pos;
static struct Rect mmi_pos_msg;

void win_mmipopup_open()
{	
	struct vpo_device *vpo_dev;

	if(get_mmi_showed()==1 && (TRUE ==GetChannelScramble()))	
	{
		api_disp_blackscreen(TRUE, TRUE);
	}
	if(get_mmi_showed()==5)
	{	
	OSD_SetColor((POBJECT_HEAD)&g_win_popup,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX);
	OSD_SetColor((POBJECT_HEAD)&win_popup_msg_mbx,WSTL_TEXT_07_HD,WSTL_TEXT_07_HD,WSTL_TEXT_07_HD,WSTL_TEXT_07_HD);
	mmi_pos_msg.uStartX = g_win_popup.head.frame.uLeft;
	mmi_pos_msg.uStartY = g_win_popup.head.frame.uTop;
	mmi_pos_msg.uWidth = g_win_popup.head.frame.uWidth;
	mmi_pos_msg.uHeight = g_win_popup.head.frame.uHeight;
	}

	if(get_mmi_showed()!=5)
		{
		OSD_SetColor((POBJECT_HEAD)&win_popup_msg_mbx,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD);
		}
	
		OSD_DrawObject((POBJECT_HEAD)&g_win_popup, C_UPDATE_ALL);
		mmipopup_is_closed=FALSE;
		mmi_pos.uStartX = g_win_popup.head.frame.uLeft;
	#ifdef CAS9_V6 //large_char
	mmi_pos.uStartY = g_win_popup.head.frame.uTop;
	#else
		mmi_pos.uStartX = g_win_popup.head.frame.uTop;
	#endif
		mmi_pos.uWidth = g_win_popup.head.frame.uWidth;
		mmi_pos.uHeight = g_win_popup.head.frame.uHeight;
	
	if(get_mmi_showed()==5)
	{			
		//win_popup_msg_mbx.head.bType=OT_MULTITEXT;
        OSD_SetColor((POBJECT_HEAD)&g_win_popup,POPUPWIN_IDX_SD,POPUPWIN_IDX_SD,0,0);
		OSD_SetColor((POBJECT_HEAD)&win_popup_msg_mbx,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD,POPUPSTR_IDX_SD);
	}

}

#ifdef SUPPORT_CAS7
void win_mmipopup_close_ex(UINT8 mmi_flag)
{
	if(get_mmi_showed()!=3&&get_mmi_showed()!=4)
	{
		if((mmipopup_is_closed==FALSE))
		{
	        OSD_ClearObject((POBJECT_HEAD)&g_win_popup, C_CLOSE_CLRBACK_FLG);
	        mmipopup_is_closed=TRUE;
		}	
	}
}	
#endif
void win_mmipopup_close(UINT8 mmi_flag)
{
	struct vpo_device *vpo_dev;
	date_time int_utc_time;

	if(get_mmi_showed()==5&&mmi_flag!=5)
		return;
		
	if(get_mmi_showed()!=3&&get_mmi_showed()!=4)
	{
		#ifdef CAS9_V6
		/* avoid mail icon overlap with host msg, in v6 we change the msg's LTWH for large character */
		ShowBMailOSDOnOff(0);
		#endif

	   if(menu_is_mmi10()&&(mmipopup_is_closed==FALSE))
	   {
	      OSD_ClearObject((POBJECT_HEAD)&g_win_popup, C_CLOSE_CLRBACK_FLG);
	      mmipopup_is_closed=TRUE;
	    }		
	}
	if(get_mmi_showed()==1)
		api_disp_blackscreen(FALSE, FALSE);
}

#ifdef SUPPORT_CAS7
#define BARGING_OFFSET 15
void win_mmipopup_msg_close(UINT8 mmi_flag,UINT8 bForce)
{
	struct vpo_device *vpo_dev;
	date_time int_utc_time;
	if(get_mmi_showed()==5&&mmi_flag!=5)
		return;
	if(get_mmi_showed()!=3&&get_mmi_showed()!=4)
	{

	if(menu_is_mmi10()&&(mmipopup_is_closed==FALSE))
	{
	g_win_popup.head.frame.uLeft = mmi_pos_msg.uStartX - BARGING_OFFSET;
	g_win_popup.head.frame.uTop = mmi_pos_msg.uStartY- BARGING_OFFSET;
	g_win_popup.head.frame.uWidth = mmi_pos_msg.uWidth + BARGING_OFFSET;
	g_win_popup.head.frame.uHeight = mmi_pos_msg.uHeight + BARGING_OFFSET;
	OSD_ClearObject((POBJECT_HEAD)&g_win_popup, C_CLOSE_CLRBACK_FLG);
	mmipopup_is_closed=TRUE;
	}


		
	}
	if(get_mmi_showed()==1)
		api_disp_blackscreen(FALSE, FALSE);
}
#endif



#ifdef SUPPORT_CAS9
void win_mmipopup_close_ex(UINT8 mmi_flag,UINT8 bForce)
{
	struct vpo_device *vpo_dev;
	date_time int_utc_time;
	if(get_mmi_showed()==5&&mmi_flag!=5)
		return;
	if(get_mmi_showed()!=3&&get_mmi_showed()!=4)
	{
		#ifdef CAS9_V6
		/* avoid mail icon overlap with host msg, in v6 we change the msg's LTWH for large character */
		ShowBMailOSDOnOff(0);
		#endif
	
		if(menu_is_mmi10()&&(mmipopup_is_closed==FALSE))
		{
            //libc_printf("\nClose popup\n");
			//OSD_ObjClose((POBJECT_HEAD)&g_win_popup,C_CLOSE_CLRBACK_FLG);
			//use saved popup frame to fix bug: when new popup after message, message frame changed to new value,
			//can't clear after new popup exit
			g_win_popup.head.frame.uLeft = mmi_pos.uStartX;
			#ifdef CAS9_V6 //large_char
			g_win_popup.head.frame.uTop = mmi_pos.uStartY;
			#else
			g_win_popup.head.frame.uTop = mmi_pos.uStartX;
			#endif
			g_win_popup.head.frame.uWidth = mmi_pos.uWidth;
			g_win_popup.head.frame.uHeight = mmi_pos.uHeight;
		
            OSD_ClearObject((POBJECT_HEAD)&g_win_popup, C_CLOSE_CLRBACK_FLG);
            mmipopup_is_closed=TRUE;
		}
        else if((mmipopup_is_closed==FALSE) && (menu_is_mmi10() == FALSE) && (bForce == TRUE)) 
        {
            POBJECT_HEAD    topmenu = menu_stack_get_top();
            while(topmenu != NULL)
            {
                OSD_ObjClose(topmenu, C_CLOSE_CLRBACK_FLG);    			
				menu_stack_pop();
				topmenu = menu_stack_get_top();
			}   
            
            OSD_ClearObject((POBJECT_HEAD)&g_win_popup, C_CLOSE_CLRBACK_FLG);
            mmipopup_is_closed=TRUE;
        }		
	}
	
	if(get_mmi_showed()==1)//&&menu_is_mmi10())
		api_disp_blackscreen(FALSE, FALSE);
}
#endif


void win_pop_msg_open(UINT16 msg_id)
{	
	if(get_mmi_showed()==1 && (TRUE ==GetChannelScramble()))	
		api_disp_blackscreen(TRUE, TRUE);

    win_pop_msg_set_display(msg_id);
	OSD_DrawObject((POBJECT_HEAD)&win_pop_msg_con, C_UPDATE_ALL);
    /*
    if(OSD_ObjOpen((POBJECT_HEAD)&win_pop_msg_con, (UINT32)msg_id) != PROC_LEAVE)
	{
		menu_stack_push((POBJECT_HEAD) &win_pop_msg_con);
	}*/
	mmipopup_is_closed=FALSE;
	mmi_pos.uStartX = g_win_popup.head.frame.uLeft;
	#ifdef CAS9_V6 //large_char
	mmi_pos.uStartY = g_win_popup.head.frame.uTop;
	#else
	mmi_pos.uStartX = g_win_popup.head.frame.uTop;
	#endif
	mmi_pos.uWidth = g_win_popup.head.frame.uWidth;
	mmi_pos.uHeight = g_win_popup.head.frame.uHeight;
}
    
void win_pop_msg_close(UINT8 mmi_flag)
{
	if(get_mmi_showed()==5&&mmi_flag!=5)
		return;
	
	if(get_mmi_showed()!=3&&get_mmi_showed()!=4)
	{
		if(menu_is_mmi10()&&(mmipopup_is_closed==FALSE))
		{
            OSD_ClearObject((POBJECT_HEAD)&win_pop_msg_con, C_CLOSE_CLRBACK_FLG);
            /*
            if(OSD_ObjClose((POBJECT_HEAD)&win_pop_msg_con, C_CLOSE_CLRBACK_FLG) == PROC_LEAVE)
			{
	 			menu_stack_pop();
			}*/
            mmipopup_is_closed=TRUE;
		}		
	}
	if(get_mmi_showed()==1)//&&menu_is_mmi10())
		api_disp_blackscreen(FALSE, FALSE);
        
}


win_popup_choice_t win_compopup_open_cas(void)
{
	CONTAINER* con =&g_win_popup;
	PRESULT bResult = PROC_LOOP;
	UINT32 hkey;
	UINT32 msg;

	win_msg_popup_close();
#ifdef CI_PLUS_SUPPORT
    if(IsCIPlus_Menu_Exist())
    {
        return WIN_POP_CHOICE_NULL;
    }
#endif
	win_popup_Init();
	//if(win_popup_type!=WIN_POPUP_TYPE_SMSG)
	{
        BOOL old_value = ap_enable_key_task_get_key(TRUE);
		while(bResult != PROC_LEAVE)
		{
			hkey = ap_get_key_msg();
			if(hkey == INVALID_HK || hkey == INVALID_MSG)
			{
				continue;
			}
			bResult = OSD_ObjProc((POBJECT_HEAD)con, (MSG_TYPE_KEY<<16),hkey, 0);//w->head.pProc((OBJECTHEAD *)w, msgcode, 0, save_back_ok);
		}
        ap_enable_key_task_get_key(old_value);
	}
	return win_popup_choice;
}

static UINT8 cas_pop_open = FALSE;
UINT8 get_cas_pop_status()
{
	return cas_pop_open;
}

win_popup_choice_t win_compopup_open_ext_cas(UINT8* back_saved)
{
	win_popup_choice_t ret;
    win_popup_type_t  type;
    type = win_popup_type;
	wincom_backup_region(&g_win_popup.head.frame);
	cas_pop_open = TRUE;
	ret = win_compopup_open_cas();
	//if(type != WIN_POPUP_TYPE_SMSG)
	{
		*back_saved = 1;
		if(win_popup_opened)
		{
			win_msg_popup_close();
		}
		wincom_restore_region();
	}
    /*
	else
	{
		*back_saved = 0;
	}
    */
    cas_pop_open = FALSE;
	return ret;
}

#endif

#if !defined(_BUILD_OTA_E_) && defined(AUTO_OTA)
 void win_otacompopup_set_msg(char* str,char* unistr,UINT16 strID)
{
	UINT8 i,btn_num,lines;
	UINT8* pstr;
	POBJECT_HEAD txt = (POBJECT_HEAD)&win_popup_msg_mbx;
	UINT16 totalw,mw,mh;
	UINT16 l,t,w,h;
	UINT8	title_flag;
	CONTAINER* win = &g_win_popup;

	title_flag = (win->pNextInCntn == txt)? 0 : 1;

	popup_mtxt_content.bTextType = STRING_UNICODE;
	popup_mtxt_content.text.pString   = msg_str;
	
	if(str!=NULL)
		ComAscStr2Uni(str, msg_str);
	else if(unistr!=NULL)
		ComUniStrCopyChar( (UINT8*)msg_str,unistr);
	else
	{
		popup_mtxt_content.bTextType        = STRING_ID;
		popup_mtxt_content.text.wStringID   = strID;
	}

	btn_num = win_popup_btm_num[win_popup_type];
	if(str != NULL || unistr!= NULL)
		pstr = (UINT8*)msg_str;
	else
		pstr = OSD_GetUnicodeString(strID);
	totalw = OSD_MultiFontLibStrMaxHW(pstr,txt->bFont,&mw,&mh,0);

	totalw += MSG_LOFFSET*2;

	if(btn_num <= 1)
		w = MIN_WIDHT_1ORNOBTN;
	else
		w = MIN_WIDTH_2BTN;
CHECK_LINES:
	lines = (totalw + w - 1) / w;
	if(lines <= 1)
		lines = 1;
	else
	{
		if(w != MAX_LEN_ONE_LINE)
		{
			w = MAX_LEN_ONE_LINE;
			goto CHECK_LINES;
		}
	}

	if(mh < DEFAULT_FONT_H)
		mh = DEFAULT_FONT_H; 
	h =  mh * lines + (lines  - 1)*4 ;
	if(btn_num == 0)
		h += MSG_VOFFSET_NOBTN*2;
	else
		h += (MSG_VOFFSET_WITHBTN + MSG_BUTTON_TOPOFFSET + MSG_BUTTON_BOTOFFSET + BTM_H);

	if(title_flag)
		h += TITLE_H + 10;
#ifdef HDOSD
	l = (OSD_HD_MAX_WIDTH - w)>>1;
	t = ((OSD_HD_MAX_HEIGHT - h)>>1) - 40;
#else
	l = (OSD_MAX_WIDTH - w)>>1;
	t = ((OSD_MAX_HEIGHT - h)>>1) - 20;
#endif
	t = (t + 1) /2 * 2; 
	win_compopup_set_frame(l,t,w,h);
}

#endif

#if(defined(MULTI_CAS)&&defined(SUPPORT_CAS_A))
void win_compopup_set_enter_wrong_pin_mmi_status(BOOL value)
{
	g_enter_wrong_pin_mmi = value;
}

BOOL  win_compopup_get_enter_wrong_pin_mmi_status()
{
	return g_enter_wrong_pin_mmi;
}

BOOL win_compopup_set_target_parental_control_level(UINT8 target_parental_control_level_code)
{
	g_target_parental_control_level_for_confirm_ui=target_parental_control_level_code;
}


BOOL win_compopup_set_target_tvpin_value(UINT8* target_tvpin)
{
	memcpy(g_target_tvpin_for_confirm_ui, target_tvpin, 8);
}

BOOL win_compopup_reset_target_tvpin_value(void)
{
	memset(g_target_tvpin_for_confirm_ui, 0xFF, 8);
}

BOOL win_compopup_get_confirm_proc_exec_result(void)
{
	return g_tvpin_confirm_proc_exec_result;
}

BOOL win_compopup_reset_confirm_proc_exec_result(void)
{
	g_tvpin_confirm_proc_exec_result=FALSE;
}

void win_compopup_set_tvpin_input_confirm_mode(BOOL bMode)
{
	g_tvpin_confirm_mode = bMode;
}

void win_compopup_set_item_name(char *str, char *unistr, UINT16 strID)
{
	TEXT_FIELD *txt;
	OBJECT_HEAD *obj= &win_popup_msg_mbx;
	OBJECT_HEAD *pObjNext;

	//libc_printf("win_compopup_set_item_name()\n");
	txt = &win_popup_item_name;
	OSD_SetTextFieldStrPoint(txt, msg_popup_item_name);
	if (str != NULL)
		OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)str);
	else if (unistr != NULL)
		OSD_SetTextFieldContent(txt, STRING_UNICODE, (UINT32)unistr);
	else
	{
		if (strID != 0)
		{
			OSD_SetTextFieldStrPoint(txt, NULL);
			OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)strID);
		}
		else
		{
			return ;
		}
	}
	OSD_SetObjpNext(obj, txt);

	pObjNext = (win_popup_btm_num[win_popup_type] <= 0) ? NULL : (POBJECT_HEAD)win_popup_btm_objs[0];
	//libc_printf("-msg_popup_item_name->pObjNext=%d\n", pObjNext);
	OSD_SetObjpNext(txt, pObjNext);

}

void win_compopup_set_item_val(char *str, char *unistr, UINT16 strID, UINT16 val_offset)
{
	TEXT_FIELD *txt;	
	OBJECT_HEAD *obj= &win_popup_msg_mbx;
	OBJECT_HEAD *pObjNext=&win_popup_item_name;
	
	//libc_printf("win_compopup_set_item_val()\n");
	txt = &win_popup_item_val;
	OSD_SetTextFieldStrPoint(txt, msg_popup_item_val);
	if (str != NULL)
		OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)str);
	else if (unistr != NULL)
		OSD_SetTextFieldContent(txt, STRING_UNICODE, (UINT32)unistr);
	else
	{
		if (strID != 0)
		{
			OSD_SetTextFieldStrPoint(txt, NULL);
			OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)strID);
		}
		else
		{	
			return ;
		}
	}
	
	obj=&win_popup_msg_mbx;	
	if(OSD_GetObjpNext(obj)==&win_popup_item_name)
	{
		obj=&win_popup_item_name;	
		OSD_SetObjpNext(obj, txt);
	}
	else
	{
		OSD_SetObjpNext(obj, txt);
	}

	//	win->pNextInCntn = (OBJECT_HEAD*)txt;

	pObjNext = (win_popup_btm_num[win_popup_type] <= 0) ? NULL : (POBJECT_HEAD)win_popup_btm_objs[0];
	OSD_SetObjpNext(txt, pObjNext);
	val_Xoffset = val_offset;
}
#endif

